package com.example.heimashop.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.heimashop.R;
import com.example.heimashop.model.Floor;
import com.example.heimashop.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

public class FloorProductAdapter extends RecyclerView.Adapter<FloorProductAdapter.ViewHolder> {

    private List<Floor.FloorProduct> productList = new ArrayList<>();
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Floor.FloorProduct product);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<Floor.FloorProduct> list) {
        this.productList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Floor.FloorProduct product = productList.get(position);
        holder.tvName.setText(product.getName());
        holder.tvPrice.setText("查看详情");
        holder.tvSales.setText("");

        if (product.getImageSrc() != null && !product.getImageSrc().isEmpty()) {
            GlideUtils.loadImage(holder.itemView.getContext(), product.getImageSrc(), holder.ivProduct);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(product);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivProduct;
        TextView tvName;
        TextView tvPrice;
        TextView tvSales;

        ViewHolder(View view) {
            super(view);
            ivProduct = view.findViewById(R.id.iv_product);
            tvName = view.findViewById(R.id.tv_name);
            tvPrice = view.findViewById(R.id.tv_price);
            tvSales = view.findViewById(R.id.tv_sales);
        }
    }
}
